<?php
class Validator{
	protected $eventList=null;
	protected $ruleUrl="";
	protected $data=null;
	
	function __construct() {
	}
	public function validate($data,$ruleUrl){
		
		$eventList=array();
		$event=null;
		$fieldList=null;
		
		$content=$this->loadRule($ruleUrl);
		
		//json解码;
		$rule=json_decode($content);
		
		
		//获得提交规则列表
		$submitRuleList=$rule->submitRuleList;
		foreach($submitRuleList as $submitRule){
			$name=$submitRule->name;
			if($name=="base"){
				$fieldList=$submitRule->fieldList;
			}
		}
	
		//验证
		foreach($fieldList as $field){
			$name=$field->name;
			$label=$field->label;
			$type=$field->type;
			$required=$field->required;
			$pattern=$field->pattern;
			$patternName=$field->patternName;
			$placeholder=$field->placeholder;
			$title=$field->title;
			$tips=$field->tips;
			$maxLength=$field->maxLength;
			
			$value=$data[$name];
			

			//验证规则开始
			$eventTips="";
			if($required){
				if($type=="select"){
					$eventTips=$label." 必选 ".$title;
				}else{
					$eventTips=$label." 必填 ".$title;
				}
			}else{
				$eventTips=$label." ".$title;
			}
			//必填验证
			if($required){
				if($value==""){
					$event=new Event();
					$event->tips=$eventTips;
					$eventList[]=$event;
					continue;
				}
			}
			//规则验证
			if($patternName!=""){
				if($pattern==""){
					$evalStr="\$pattern=PatternBuilder::".$patternName.";";
					eval("$evalStr");
				}
			}
			if($pattern!=""){
				if($type!="select"){
					if(!preg_match("/".$pattern."/",$value)){
						$event=new Event();
						$event->tips=$eventTips;
						$eventList[]=$event;
					}
				}
			}
			//验证规则结束
		}
		$this->eventList=$eventList;
		if(count($eventList)==0){
			return true;	
		}else{
			return false;
		}
	}
	protected function loadRule($path){
		$content=file_get_contents($path);
		return $content;
	}
	public function getEventList(){
		return $this->eventList;
	}
}
class Event{
	public $tips="";
	public $target="";
	public $code="";
}
class PatternBuilder{
	public static function text($min,$max){
		$str="^*[\\s\\S]{".$min.",".$max."}$";
		return $str;
	}
	public static function content($min,$max){
		$str="^[\\s\\S]{".$min.",".$max."}$";
		return $str;
	}
	public static function letterNumber($min,$max){
		$str="^[a-zA-Z0-9]{".$min.",".$max."}$";
		return $str;
	}
	public static function letter($min,$max){
		$str="^[a-zA-Z]{".$min.",".$max."}$";
		return $str;
	}
	public static function number($min,$max){
		$str="^[0-9]{"+$min+","+$max+"}$";
		return $str;
	}
	public static function float($integer,$float){
		$str="^((([1-9]\\d*)|0)(\\.\\d{1,2})?)$";
		return $str;
	}
	public static function email(){
		$str="^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
		return $str;
	}
	public static function tel(){
		$str="^.{1,20}$";
		return $str;
	}
	public static function mobile(){
		$str="^[0-9]{11,11}$";
		return $str;
	}
	public static function unequal($str){
		//^(?!0$)
		$str="^(?!".$str."$)";
		return $str;
	}
}


$data=$_POST;
$validator =new Validator();
$result=$validator->validate($data,"./formRule.json");

if(!$result){
	$eventList=$validator->getEventList();
	print_r($eventList);
}
?>